// components/popup/payment/index.js
import User from '../../../utils/user';
import util from '../../../utils/utils';
import md5 from "../../../utils/md5";
let phone = User.UserData.phone;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    pwdVal:'',
    pwdText:'',
    payFocus:false,
    userInfo:User.UserData,
    phone:phone,
    checkedIcon: {
      active: '/images/icon/icon_check_active.png',
      normal: '/images/icon/icon_check_inactive.png',
      normalEmpty: '/images/icon/icon_check_inactive_empty.png'
    },
    detail:{
      code:'',
      pwd:'',
      repwd:''
    },
    smsTip: '获取验证码',
    timeCount:30,
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onClose: function () {
      this.setData({
        show: false,
      });
      this.triggerEvent('close')
    },
    onChangeNotPwd:function(event) {
      this.setData({ isCheckNotPwd: event.detail });
    },
    /**
     * 密码输入获取焦点
     */
    getFocus: function (e) {
      this.setData({ payFocus: true });
    },
    setFormValue:function(event){
      let self = this;
      let _value = event.detail.value;
      let _detail = self.data.detail;
      let name = event.currentTarget.dataset.name;
      if(name){
        if(_detail[name] !== _value){
          _detail[name] = _value;
          self.setData({
            detail:_detail
          })
        }
      }
    },
    getSmsCode: function() {
      let self = this;
      util.request({
        url: util.api.SendSMS,
        data: {
          phone: this.data.phone
        },
        handle: true,
        error: function(res) {
          util.msg('服务器异常,请稍候再试');
        },
        callback: function(res) {
          let timeKey = null;
          if (res.status === 'ok') {
            self.setData({
              smsTip: '30S后可重发',
              timeCount:30,
            });
            timeKey = setInterval(function() {
              if (self.data.timeCount > 0) {
                self.setData({
                  timeCount: self.data.timeCount - 1,
                  smsTip: self.data.timeCount + 'S后可重发'
                });
              } else {
                clearInterval(timeKey);
                self.setData({
                  timeCount:30,
                  smsTip: '获取验证码'
                });
              }
            }, 1000);
            util.msg('验证码发送成功,请注意查收!');
          }else{
            util.msg(res.msg);
          }
        }
      });
    },
    submitPwd:function (event) {
      let self  = this;
      let _detail = self.data.detail;
      let url = 's/userinfo/ResetPayPassword';
      if(_detail.code === ''){
        util.msg('请输入短信验证码!');
        return false;
      }
      if(_detail.pwd === ''){
        util.msg('请填写要设置的支付密码!');
        return false;
      }
      if(_detail.repwd === ''){
        util.msg('请再次输入支付密码!');
        return false;
      }
      if(_detail.repwd !== _detail.pwd){
        util.msg('两次支付密码不一致!');
        return false;
      }
      _detail['pwd'] = md5(_detail['pwd']);
      _detail['pwd'] = _detail.pwd.toUpperCase();
      /**
       * 提交到服务器
       */
      util.request({
        url:url,
        data:_detail,
        callback:function (result) {
          util.msg('支付密码设置成功!');
          self.setData({
            show: false,
          });
          self.triggerEvent('close')
        }
      });
    },
  }
});
